package com.erp.erp_ui.base;

import com.erp.utils.DateUtils;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.erp.utils.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;


/**
 * @ClassName GenerateNumberControl
 * @Description 按时间生成编号
 */
@Controller
@RequestMapping(value = "number")
 public class CommonNumberControl {

    /**
     * 返回编号
     * @return
     */
    @RequestMapping(value = "numberByTypeAndDate",method = RequestMethod.POST)
    @ResponseBody
     public R<String> generateNumberByDate(@RequestBody Map<String, Object> map)  {
        try {
            if(map==null || map.get("type")==null){
                return  R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            String type = !"".equals(map.get("type"))?map.get("type").toString():"";
            Date date = new Date();
            if (map.get("time")!=null &&!"".equals(map.get("time"))){
                try{
                    date = DateUtils.convertStringToDate(map.get("time").toString(),"yyyy-MM-dd HH:mm:ss.SSS");
                }catch (Exception e1){
                    return R.buildError(ResultStatusEnum.TIME_EMPTY);
                }
            }
            String oddNumber = StringUtil.uniqueOddNumber(type, date);
            return R.build(oddNumber);
        }catch (Exception e){
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取当前月份 开始结束日期
     * @return
     */
    @RequestMapping(value = "getNowMonthNew",method = RequestMethod.POST)
    @ResponseBody
    public R getNowMonthNew(){
        try {
            Map<String, Date> map = DateUtils.getNowMonthNew();
            return R.build(map);
        }catch (Exception e){
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
}
